package com.mgg.goods.service.impl;

import com.github.pagehelper.PageHelper;
import com.mgg.common.enums.ResultEnum;
import com.mgg.common.dao.DaoSupport;
import com.mgg.common.exception.ServiceException;
import com.mgg.goods.entity.GoodsAttrDO;
import com.mgg.goods.entity.GoodsDO;
import com.mgg.goods.entity.GoodsSpecDO;
import com.mgg.goods.enums.GoodsResultEnum;
import com.mgg.goods.service.BaseGoodsService;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: lijun
 * @Date: 2018/9/11 9:44
 */
@Service("baseGoodsService")
public class BaseGoodsServiceImpl implements BaseGoodsService {

    @Resource(name = "daoSupport")
    private DaoSupport dao;

    @Override
    public GoodsDO getGoodsById(String goodsId) {
        GoodsDO goods = dao.findForObject("BaseGoodsMapper.getGoodsById",goodsId);
        if (goods == null) {
            throw new ServiceException(GoodsResultEnum.GOODS_NOT_EXIST.getCode(),GoodsResultEnum.GOODS_NOT_EXIST.getMessage());
        }
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("goodsId", goodsId);
        queryMap.put("superId", "0");
        List<GoodsAttrDO> attrs = dao.findForList("BaseGoodsMapper.listGoodsAttr", queryMap);
        for (GoodsAttrDO attr : attrs) {
            queryMap.put("superId", attr.getAttrId());
            attr.setItems(dao.findForList("BaseGoodsMapper.listGoodsAttr",queryMap));
        }
        goods.setAttrs(attrs);
        List<GoodsSpecDO> specs = dao.findForList("BaseGoodsMapper.listGoodsSpec", goodsId);
        goods.setSpecs(specs);
        return goods;
    }

    @Override
    public List<GoodsDO> listGoodsByCategoryId(String categoryId, String sort, String status, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("categoryId", categoryId);
        map.put("sort", sort);
        map.put("status", status);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByCategoryId",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listHotGoodsByFirstCategoryId(String categoryId, String sort, String status) {
        Map<String,Object> map = new HashMap<>();
        map.put("categoryId", categoryId);
        map.put("sort", sort);
        map.put("status", status);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listHotGoodsByFirstCategoryId",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listGoodsByFirstCategoryId(String categoryId, String sort, String status, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("categoryId", categoryId);
        map.put("sort", sort);
        map.put("status", status);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByFirstCategoryId",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listGoodsByPlateId(String plateId, String is_rand, String sort, String status, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("plateId", plateId);
        map.put("sort", sort);
        map.put("is_rand", is_rand);
        map.put("status", status);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByPlateId",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listGoodsByPlateType(Integer plateType, String sort, String status, Pageable page) {
        if(page != null){
            PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        }
        Map<String,Object> map = new HashMap<>();
        map.put("plateType", plateType);
        map.put("sort", sort);
        map.put("status", status);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByPlateType",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listGoodsByName(String name, String sort, String status, String goodsType, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("name", name);
        map.put("sort", sort);
        map.put("status", status);
        map.put("goodsType",goodsType);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByName",map);
        return goodsList;
    }

    @Override
    public List<GoodsDO> listGoodsByStoreCode(String storeCode, String sort, String status, Boolean isRecommend, Pageable page) {
        PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        Map<String,Object> map = new HashMap<>();
        map.put("storeCode", storeCode);
        map.put("sort", sort);
        map.put("status", status);
        map.put("isRecommend", isRecommend);
        List<GoodsDO> goodsList = dao.findForList("BaseGoodsMapper.listGoodsByStoreCode",map);
        return goodsList;
    }
}
